package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IWatehouseMaterialService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IWarehousingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 入库单Controller
 * 
 * @author ruoyi
 * @date 2021-05-08
 */
@RestController
@RequestMapping("/system/warehousing")
public class WarehousingController extends BaseController
{
    @Autowired
    private IWarehousingService warehousingService;

    @Resource
    private com.ruoyi.system.service.IWatehouseMaterialService IWatehouseMaterialService;
    /**
     * 查询入库单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:list')")
    @GetMapping("/list")
    public TableDataInfo list(Warehousing warehousing)
    {
        startPage();
        List<Warehousing> list = warehousingService.selectWarehousingList(warehousing);
        return getDataTable(list);
    }

    /**
     * 查询入库单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:list')")
    @GetMapping("/QueryWarehousing")
    public TableDataInfo QueryWarehousing(Warehousing warehousing)
    {
        startPage();
        warehousing.setOprType(0);
        List<Warehousing> list = warehousingService.selectQueryWarehousing(warehousing);
        return getDataTable(list);
    }

    /**
     * 查询出库单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:list')")
    @GetMapping("/QueryOuting")
    public TableDataInfo QueryOuting(Warehousing warehousing)
    {

        startPage();
        warehousing.setOprType(1);
        List<Warehousing> list = warehousingService.selectQueryWarehousing(warehousing);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:export')")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Warehousing warehousing)
    {
        List<Warehousing> list = warehousingService.selectWarehousingList(warehousing);
        ExcelUtil<Warehousing> util = new ExcelUtil<Warehousing>(Warehousing.class);
        return util.exportExcel(list, "入库单数据");
    }

    /**
     * 获取入库单详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:warehousing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(warehousingService.selectWarehousingById(id));
    }

    /**
     * 获取出库单详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:warehousing:query')")
    @GetMapping(value = "/outing/{id}")
    public AjaxResult outIng(@PathVariable("id") Long id)
    {
        return AjaxResult.success(warehousingService.selectOutingById(id));
    }

    /**
     * 新增入库单
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:add')")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Warehousing warehousing)
    {
        return toAjax(warehousingService.insertWarehousing(warehousing));
    }

    /**
     * 修改入库单
     */
    //@PreAuthorize("@ss.hasPermi('system:warehousing:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehousing warehousing)
    {
        return toAjax(warehousingService.updateWarehousing(warehousing));
    }



    /**
     * 删除出库单
     */

    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/outing")
    public AjaxResult removeOut(@RequestBody Warehousing warehousing)
    {
        System.out.println(warehousing);
        int i = warehousingService.deleteWarehousingById(warehousing);

        if (i==0){
            return error("入库单中材料已经入库，无法删除");
        }
        if (i==1){
            return success("删除成功");
        }
        if (i==2){
            return error("出库单中已有材料出库，无法删除");
        }
        if (i==3){
            return success("删除成功");
        }
        return success();

    }

    //一键入库
    @PutMapping("/intoWare")
    public AjaxResult intoWare( @RequestBody IntoWare[] intoWare)
    {
        System.out.println(intoWare);
        return toAjax(warehousingService.intoWare(intoWare));
    }


    //一键出库
    @PutMapping("/outWare")
    public AjaxResult outWare(@RequestBody IntoWare[] intoWare)
    {
        return toAjax(warehousingService.outWare(intoWare));
    }
//
//器具导出
@PostMapping("/uplod")
@CrossOrigin
public void uplod(Long id, HttpServletResponse response){

    try {
        Warehousing warehousing = warehousingService.selectWarehousingById(id);
        List<Object> list = new ArrayList<>();
        // 表一数据
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat( "yyyy-MM-dd");
        for (int i = 0; i < warehousing.getPurchaseRecords().size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i+1);
            map.put("mName",warehousing.getPurchaseRecords().get(i).getmName());
            map.put("name",warehousing.getPurchaseRecords().get(i).getmModel()+"/"+warehousing.getPurchaseRecords().get(i).getMdSpecifications());
            map.put("mdBrand",warehousing.getPurchaseRecords().get(i).getMdBrand());
            map.put("unit",warehousing.getPurchaseRecords().get(i).getmUnit());
            map.put("sum",warehousing.getPurchaseRecords().get(i).getMdSum());
            map.put("remark",warehousing.getPurchaseRecords().get(i).getRemark());
            list.add(map);
        }
        // 表格使用的数据
        Map map = new HashMap();

        for (int i = list.size(); i < 15; i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", list.size() + 1);
            map1.put("mName", "");
            map1.put("name", "");
            map1.put("mUnit", "");
            map1.put("mdBrand", "");
            map1.put("mdBusiness", "");
            map1.put("commBus", "");
            map1.put("mdSum", "");
            map1.put("mdPrice", "");
            map1.put("sum", "");
            list.add(map1);
        }
        map.put("data", list);//放入表一数据
        map.put("proName", warehousing.getProName());//放入表一数据
        map.put("proNumber", warehousing.getProNumber());//放入表一数据
        // 获取模板文件
        //读取模板
        TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/入库单.xlsx");
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        workbook.write(response.getOutputStream());
        workbook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    //删除具体材料
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchaseRecord purchaseRecord)
    {
        System.out.println(purchaseRecord);
        return toAjax(warehousingService.delete(purchaseRecord));
    }

}
